import ble from '../../utils/bluetooth'
import a2b from '../../utils/str2hex2ab'
let mode = ''
Page({
    data: {
        color: ['#de86e6', '#e93225', '#64ad75', '#0100f2', '#fdff54'],
        nowValue: [21, 43, 56, 73, 12],
        autoValue: 33
    },
    onLoad: function () {

    },
    // 手动模式下
    async sliderchange(e) {
        let index = e.currentTarget.dataset.index
        let all = e.currentTarget.dataset.all
        let value = e.detail.value
        console.log(all)
        if (all) {
            this.setData({
                autoValue: e.detail.value
            })
            for (let i = 0; i < 5; i++) {
                this.setData({
                    [`nowValue[${i}]`]: value
                })
            }
        } else {
            this.setData({
                [`nowValue[${index}]`]: value
            })
        }
        // 1、先切换到手动模式
        await this.modeToUnAuto()
        // if (mode != 'unAuto') {
        //   let res = await this.modeToUnAuto()
        //   console.log('res', res)
        // }
        // 2、发送当前滑动数值到模块上
        let data = ['0x4c', '0x45', '0x44', '0x00', '0x00', '0x00', '0x00', '0x00']
        for (const i in this.data.nowValue) {
            data[parseInt(i) + 3] = '0x' + formatNumber(this.data.nowValue[i].toString(16))
        }
        // let newValue = '0x' + formatNumber(value.toString(16))
        // data[index] = newValue
        console.log(data)
        let res = await ble.writePrescriptiveDirective(data)
        console.log(res)
    },
    async operation2(e) {
        let all = e.currentTarget.dataset.all
        let i = e.currentTarget.dataset.i
        let index = e.currentTarget.dataset.index
        let num = 0
        if (all) {
            num = this.data.autoValue
        } else {
            num = this.data.nowValue[index]
        }
        if ((i > 0 && num < 100) || (i < 0 && num > 0)) {
            num += i
        } else if ((i < 0 && num == 0) || (i > 0 && num == 100)) {
            return
        }
        if (all) {
            this.setData({
                autoValue: num
            })
            for (let i = 0; i < 5; i++) {
                this.setData({
                    [`nowValue[${i}]`]: num
                })
            }
        } else {
            this.setData({
                [`nowValue[${index}]`]: num
            })
        }
        // 1、先切换到手动模式
        await this.modeToUnAuto()
        // if (mode != 'unAuto') {
        //   let res = await this.modeToUnAuto()
        //   console.log('res', res)
        // }
        // 2、发送当前滑动数值到模块上
        let data = ['0x4c', '0x45', '0x44', '0x00', '0x00', '0x00', '0x00', '0x00']
        console.log(this.data.nowValue)
        for (const i in this.data.nowValue) {
            data[parseInt(i) + 3] = '0x' + formatNumber(this.data.nowValue[i].toString(16))
        }
        console.log("data", data)
        let res = await ble.writePrescriptiveDirective(data)
        console.log(res)
    },
    // switch开关
    async switchChange(e) {
        if (e.detail.value) {
            this.setData({
                checked: true
            })
            await this.modeToUnAuto()
            console.log('开机')
        } else {
            this.setData({
                checked: false,
            })
            await this.modeToShutDown()
            console.log('关机')
        }
    },
    // 自动模式下
    // async sliderAuto(e) {
    //     console.log(e)
    //     this.setData({
    //         autoValue: e.detail.value
    //     })
    //     // 1、先切换到自动模式
    //     if (mode != 'auto') {
    //         let res = await this.modeToAuto()
    //         console.log('res', res)
    //     }

    //     // 2、发送当前滑动数值到模块上
    //     let data = ['0x4c', '0x45', '0x44', '0x00', '0x00', '0x00', '0x00', '0x00']
    //     let newValue = '0x' + formatNumber(e.detail.value.toString(16))
    //     for (const i in data) {
    //         if (i > 2) {
    //             data[i] = newValue
    //         }
    //     }
    //     let res = await ble.writePrescriptiveDirective(data)
    //     console.log(res)
    // },
    // 将模式切换到手动模式
    async modeToUnAuto() {
        mode = 'unAuto'
        let data = ['0x4d', '0x4f', '0x44', '0x00']
        return await ble.writePrescriptiveDirective(data)
    },
    // 将模式切换到自动模式
    async modeToAuto() {
        mode = 'auto'
        let data = ['0x4d', '0x4f', '0x44', '0x01']
        return await ble.writePrescriptiveDirective(data)
    },
    // 将模式切换到关机模式
    async modeToShutDown() {
        mode = 'shutdown'
        let data = ['0x4d', '0x4f', '0x44', '0x02']
        return await ble.writePrescriptiveDirective(data)
    }
})

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}